package com.xunyji.demo03.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author AltEnter
 * @create 2019-01-21 21:38
 * @desc 自定义简介Realm
 **/
public class MySimpleRealm02 implements Realm {

    /** 用户名 */
    public static final String USERNAME = "fury02";
    /** 用户密码 */
    public static final String PASSWORD = "222222";

    public String getName() {
        return "MySimpleRealm02";
    }

    public boolean supports(AuthenticationToken token) {
        if (token instanceof UsernamePasswordToken) {
            return true;
        }
        return false;
    }

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        if (USERNAME.equals(username) && PASSWORD.equals(password)) {
            return new SimpleAuthenticationInfo(username, password, getName());
        } else {
            throw new RuntimeException("MySimpleRealm02 - 用户名或者密码错误");
        }
    }
}

